package com.alvis.media.utility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 *@author 奇趣
 */
public final class DateTimeUtil
{
    private static final Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);
    public static final String STANDER_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String STANDER_SHORT_FORMAT = "yyyy-MM-dd";

    /**
     * 获取年月日
     */
    public static String getCurrentTimeYMD()
    {
        // 获取当前日期（年月日）
        LocalDate currentDate = LocalDate.now();

        // 定义格式器：yyyy-MM-dd（年-月-日，月份和日期不足2位补0）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 格式化日期为字符串
        String formattedDate = currentDate.format(formatter);

        return formattedDate; // 输出类似：2025-10-22（根据当前系统时间动态变化）
    }

    public static Date addDuration(Date date, Duration duration) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.SECOND, (int) duration.getSeconds());
        return ca.getTime();
    }

    /**
     * 将日期类型的时间转换为 字符串时间yyyy-MM-dd hh:mm:ss
     */
    public static String dateFormat(Date date)
    {
        if (null == date) {
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat(STANDER_FORMAT);
        return dateFormat.format(date);
    }

    /**
     * 将日期类型的时间转换为 字符串时间yyyy-MM-dd
     */
    public static String dateShortFormat(Date date)
    {
        if (null == date)
        {
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat(STANDER_SHORT_FORMAT);
        return dateFormat.format(date);
    }

    /**
     * 将字符串时间转换为日期类型的时间
     */
    public static Date parse(String dateStr, String format) {
        try {
            return new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
//    public static void main(String str[])
//    {
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getMonthStartDay()));
//
//    }

    /**
     * 获取系统当月的第一天 比如：当前时间2025-10-24 00:00:00 转换成2025-10-01 00:00:00
     */
    public static Date getMonthStartDay()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String dateStr = formatter.format(cale.getTime());
        return parse(dateStr, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     *
     * 获取系统当月的最后1天 比如：当前时间2025-10-24 11:23:51 转换成2025-10-31 23:59:59
     */
    public static Date getMonthEndDay()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String dateStr = formatter.format(cale.getTime());
        return parse(dateStr, STANDER_FORMAT);
    }

    /**
     * 获取系统当月的第1天~系统当前月的时间  2025-10-01~2025-10-24中的每一天
     */
    public static List<String> mothStartToNowFormat()
    {
        Date startTime = getMonthStartDay();
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(new Date());
        int mothDayCount = nowCalendar.get(Calendar.DAY_OF_MONTH);
        List<String> mothDays = new ArrayList<>(mothDayCount);
        Calendar startCalendar = new GregorianCalendar();
        startCalendar.setTime(startTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        mothDays.add(formatter.format(startTime));
        for (int i = 0; i < mothDayCount - 1; i++) {
            startCalendar.add(Calendar.DATE, 1);
            Date end_date = startCalendar.getTime();
            mothDays.add(formatter.format(end_date));
        }
        return mothDays;
    }

    /**
     * 获取系统当月的第1天~系统当前月的最后1天 系统当前月的时间  2025-10-24 获取到的是
     * 1 ~ 31 号中的每一天 1 2 3 ... 31日
     */
    public static List<String> monthDay()
    {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(getMonthEndDay());
        int endMothDay = endCalendar.get(Calendar.DAY_OF_MONTH);
        List<String> list = new ArrayList<>(endMothDay);
        for (int i = 1; i <= endMothDay; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }
}
